using System;
using System.Xml;

using ScenarioFramework.WorkflowEngine.DataModel;
using ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.MetaWorkflow
{
	//*********************************************************************
	//
	// XMLMetaWorkflowActivity Class
	//
	// The MetaWorkflowActivity class represents a pre-workflow step
	// of a selection procedure for configuring a product
	//
	//*********************************************************************

	public class XMLMetaWorkflowActivity : bOSdotNET.XML.Data.XMLObject
	{
		public const string MAIN_NODE	= "Activity";

		private const string NAME_ATTRIBUTE		= "Name";
		private const string DESCRIPTION_NODE	= "Description";
		private const string HELP_URL_NODE	= "HelpUrl";
		private const string CONDITION_NODE = "Condition";

		/** FIELDS **/

		private string sCondition = string.Empty;
		private string sName;
		private string sDescription;
		private string sHelpUrl = string.Empty;

		private ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Form.XMLForm form;
		private ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities vEntities; 

		private XMLAttributesRelationShips vRelations;
		private Rule.XMLRules vRules;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLMetaWorkflowActivity() 
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLMetaWorkflowActivity(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		public ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Form.XMLForm XMLForm 
		{
			get { return form; }
			set { form = value; }
		}

		public ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities XMLEntities
		{
			get { return vEntities; }
			set { vEntities = value; }
		}

		public XMLAttributesRelationShips XMLAttributesRelationShips 
		{
			get { return vRelations; }
			set { vRelations = value; }
		}

		public Rule.XMLRules XMLRules
		{
			get { return vRules; }
			set { vRules = value; }
		}

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Condition 
		{
			get { return sCondition; }
			set { sCondition = value; }
		}

		public string HelpUrl
		{
			get { return sHelpUrl; }
			set { sHelpUrl = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			sName=string.Empty;
			sDescription=string.Empty;

			form = new Form.XMLForm();
			vEntities = new Entity.XMLEntities();

			vRelations = new XMLAttributesRelationShips();
			vRules = new Rule.XMLRules();
		}

		/** Parsering method for filling the mwf-step **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);
				switch ( node.Name )
				{
					case XMLMetaWorkflowActivity.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
				}
			}

			/** find the sub tags witch contains mwf values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLMetaWorkflowActivity.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;

					case XMLMetaWorkflowActivity.HELP_URL_NODE:
						sHelpUrl= node.InnerText;
						break;

					case XMLMetaWorkflowActivity.CONDITION_NODE:
						sCondition= node.InnerText;
						break;

					case Form.XMLForm.MAIN_NODE:
						//TODO: Eseguire il loadind del form????
						form.Parse (node);
						break;

					case Entity.XMLEntities.MAIN_NODE:
						vEntities.Parse(node);
						break;

					case XMLAttributesRelationShips.MAIN_NODE:
						vRelations.Parse(node);
						break;

					case Rule.XMLRules.MAIN_NODE:
						vRules.Parse(node);
						break;
				} 
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLMetaWorkflowActivity.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLMetaWorkflowActivity.NAME_ATTRIBUTE, sName) );
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLMetaWorkflowActivity.DESCRIPTION_NODE, sDescription) );
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLMetaWorkflowActivity.CONDITION_NODE, sCondition) );

			//xmlNode.AddNode ( form.GetXMLNode() );
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (Form.XMLForm.MAIN_NODE, form.Name));
			xmlNode.AddNode ( vEntities.GetXMLNode() );
			xmlNode.AddNode ( vRelations.GetXMLNode() );
			xmlNode.AddNode ( vRules.GetXMLNode() );

			return xmlNode;
		}

	}
}
